package com.anywhere.freightman.car.controller;

import com.anywhere.freightman.car.controller.request.CarDriverRequest;
import com.anywhere.freightman.car.controller.request.CarRequest;
import com.anywhere.freightman.car.controller.resp.SimpleCarResp;
import com.anywhere.freightman.car.model.CarModel;
import com.anywhere.freightman.car.service.ICarService;
import com.anywhere.freightman.common.ContextKeys;
import com.anywhere.freightman.common.annotation.Permission;
import com.anywhere.freightman.common.entity.ReturnResp;
import com.anywhere.freightman.common.entity.car.CarQueryEntity;
import com.anywhere.freightman.common.entity.user.Account;
import com.anywhere.freightman.common.enumeration.CarStatus;
import com.anywhere.freightman.common.enumeration.RoleType;
import com.anywhere.freightman.common.ex.CustomException;
import com.anywhere.freightman.common.request.PageRequest;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("car")
@Api(tags = "车辆管理")
public class CarController {

    @Autowired
    private ICarService carService;

    @Permission(code = "CAR")
    @GetMapping("page")
    @ApiOperation("分页")
    public PageInfo<CarModel> getPageList(HttpServletRequest httpRequest,
                                          CarQueryEntity qm) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);

        PageRequest<CarQueryEntity> pageRequest = new PageRequest<>();
        if (!account.getType().equals(RoleType.ADMIN)) {
            qm.setCompanyId(account.getCompanyId());
        }
        BeanUtils.copyProperties(qm, pageRequest);
        pageRequest.setEntity(qm);
        return carService.getPageList(pageRequest);
    }

    @Permission(code = "CAR")
    @GetMapping("{id}")
    @ApiOperation("详情")
    public ReturnResp getDetails(HttpServletRequest httpRequest,
                                 @PathVariable("id") Long id) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        CarModel carModel = carService.getById(id);
        if (carModel == null || (!account.getType().equals(RoleType.ADMIN) && !account.getCompanyId().equals(carModel.getCompanyId()))) {
            throw new CustomException("车辆信息不存在");
        }
        return ReturnResp.buildOk(carModel);
    }

    @PostMapping
    @ApiOperation("添加车辆")
    @Permission(code = "CAR")
    public ReturnResp addCar(HttpServletRequest httpRequest,
                             @RequestBody CarModel model) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        if (!account.getType().equals(RoleType.ADMIN)) {
            model.setCompanyId(account.getCompanyId());
        }
        model.setInsertTime(new Date());
        model.setStatus(CarStatus.IDLE);
        carService.insert(model);
        return ReturnResp.buildOk(model);
    }

    @PutMapping
    @ApiOperation("编辑车辆")
    @Permission(code = "CAR")
    public ReturnResp editCar(HttpServletRequest httpRequest,
                              @RequestBody CarRequest entity) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);

        CarModel carModel = carService.getById(entity.getId());
        if (carModel == null || (!account.getType().equals(RoleType.ADMIN) && !account.getCompanyId().equals(carModel.getCompanyId()))) {
            throw new CustomException("车辆信息不存在");
        }
        BeanUtils.copyProperties(entity, carModel);
        carService.update(carModel);
        return ReturnResp.buildOk(carModel);
    }

    @GetMapping("driver")
    @ApiOperation("查询司机列表")
    @Permission(code = "CAR")
    public ReturnResp getDriverList(@RequestParam("carId") Long carId) {
        return ReturnResp.buildOk(carService.getDriverList(carId));
    }

    @PostMapping("driver/bind")
    @ApiOperation("绑定司机")
    @Permission(code = "CAR")
    public ReturnResp bindDriver(HttpServletRequest httpRequest,
                                 @RequestBody CarDriverRequest request) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        carService.bindDriver(request.getCarId(), request.getDriverId());
        return ReturnResp.buildOk();
    }

    @PostMapping("driver/remove")
    @ApiOperation("解绑司机")
    @Permission(code = "CAR")
    public ReturnResp removeDriver(HttpServletRequest httpRequest,
                                   @RequestBody CarDriverRequest request) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        carService.removeDriver(request.getCarId(), request.getDriverId());
        return ReturnResp.buildOk();
    }

    @GetMapping("my")
    @ApiOperation("我的车辆列表")
    @Permission(code = "CAR")
    public ReturnResp getMyCarList(HttpServletRequest httpRequest) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        List<SimpleCarResp> response = new ArrayList<>();
        List<CarModel> carList = carService.getListByCompanyId(account.getCompanyId());
        carList.forEach(item -> {
            SimpleCarResp resp = new SimpleCarResp(item.getId(), item.getCarNumber(), item.getCompanyId());
            response.add(resp);
        });
        return ReturnResp.buildOk(response);
    }

    @GetMapping("my/trailer")
    @ApiOperation("我的挂车列表")
    @Permission(code = "CAR")
    public ReturnResp getMyTrailerList(HttpServletRequest httpRequest) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        List<SimpleCarResp> response = new ArrayList<>();
        List<CarModel> carList = carService.getTrailerListByCompanyId(account.getCompanyId());
        carList.forEach(item -> {
            SimpleCarResp resp = new SimpleCarResp(item.getId(), item.getCarNumber(), item.getCompanyId());
            response.add(resp);
        });
        return ReturnResp.buildOk(response);
    }

    @DeleteMapping("{id}")
    @ApiOperation("注销车辆")
    @Permission(code = "CAR")
    public ReturnResp removeCar(@PathVariable("id") long carId) {
        carService.remove(carId);
        return ReturnResp.buildOk();
    }

}
